//
//  iPodLibraryExampleViewController.m
//  iPodLibraryExample
//

#import "iPodLibraryExampleViewController.h"

@implementation iPodLibraryExampleViewController

- (void)showPicker:(id)sender {

	MPMediaPickerController *picker = [[MPMediaPickerController alloc]
									   initWithMediaTypes:MPMediaTypeAny];
	picker.delegate = self;
	picker.allowsPickingMultipleItems = YES;
	picker.prompt = @"Please select";
	[self presentModalViewController:picker animated:YES];
	[picker release];
}

- (void)createCurrentTrackDisplay {
	
	currentArtwork = [[UIImageView alloc] init];
	[currentArtwork setFrame:CGRectMake(10.0, 10.0, 180.0, 180.0)];
	currentArtwork.backgroundColor = [UIColor whiteColor];
	[self.view addSubview:currentArtwork];
	[currentArtwork release];
	
	currentArtist = [[UILabel alloc] init];
	[currentArtist setFrame:CGRectMake(10.0, 200.0, 300.0, 24.0)];
	currentArtist.backgroundColor = [UIColor clearColor];
	[self.view addSubview:currentArtist];
	[currentArtist release];
	
	currentSong = [[UILabel alloc] init];
	[currentSong setFrame:CGRectMake(10.0, 230.0, 300.0, 24.0)];
	currentSong.backgroundColor = [UIColor clearColor];
	[self.view addSubview:currentSong];
	[currentSong release];
	
	trackCount = [[UILabel alloc] init];
	[trackCount setFrame:CGRectMake(10.0, 260.0, 300.0, 24.0)];
	trackCount.backgroundColor = [UIColor clearColor];
	[self.view addSubview:trackCount];
	[trackCount release];
}

- (void)createVolumeControl {
	
	MPVolumeView *volumeView = [[MPVolumeView alloc] init];
	[volumeView setFrame:CGRectMake(10.0, 360.0, 300.0, 34.0)];
	[self.view addSubview:volumeView];
	[volumeView release];
}

- (void)prevNextAction:(id)sender {
	
	// 이전 곡 버튼인지 다음 곡 버튼인지 확인하고 이동
	if ([sender tag] == 0 && songNumber > 0) {
		player.skipToPreviousItem;
		songNumber--;
	}
	
	if ([sender tag] == 1 && songNumber < songCount-1) {
		player.skipToNextItem;
		songNumber++;
	}
}

- (void)mediaPickerAction:(id)sender {
	
	MPMediaPickerController *picker = [[MPMediaPickerController alloc]
									   initWithMediaTypes:MPMediaTypeAny];
	picker.delegate = self;
	picker.allowsPickingMultipleItems = YES;
	picker.prompt = @"Select tracks";
	[self presentModalViewController:picker animated:YES];
	[picker release];
}

- (void)createPrevNextButtons {
	
	UIButton *btnPrev = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnPrev setFrame:CGRectMake(10.0, 410.0, 45.0, 34.0)];
	[btnPrev setTag:0];
	[btnPrev setTitle:@"<" forState:UIControlStateNormal];
	[btnPrev addTarget:self action:@selector(prevNextAction:)
	  forControlEvents:UIControlEventTouchUpInside];
	
	[self.view addSubview:btnPrev];
	
	UIButton *btnNext = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnNext setFrame:CGRectMake(60.0, 410.0, 45.0, 34.0)];
	[btnNext setTag:1];
	[btnNext setTitle:@">" forState:UIControlStateNormal];
	[btnNext addTarget:self action:@selector(prevNextAction:)
	  forControlEvents:UIControlEventTouchUpInside];
	
	[self.view addSubview:btnNext];
}

- (void)createMediaPickerButton {
	
	UIButton *btnMedia = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnMedia setFrame:CGRectMake(230.0, 410.0, 80.0, 34.0)];
	[btnMedia setTitle:@"Media" forState:UIControlStateNormal];
	[btnMedia addTarget:self action:@selector(mediaPickerAction:)
	   forControlEvents:UIControlEventTouchUpInside];
	
	[self.view addSubview:btnMedia];
}
 
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker
  didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
	
	[self dismissModalViewControllerAnimated:YES];
	
	[player setQueueWithItemCollection:mediaItemCollection];
	
	songCount = [[mediaItemCollection items] count];
	songNumber = 0;
	
	if (songCount > 0)
		[player play];
}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
	[self dismissModalViewControllerAnimated:YES];
}

- (void)songChanged:(NSNotification *)aNotification {
	
	MPMediaItem *item = [player nowPlayingItem];
	currentArtist.text = [item valueForProperty:MPMediaItemPropertyArtist];
	currentSong.text = [item valueForProperty:MPMediaItemPropertyTitle];
	trackCount.text = [NSString stringWithFormat:@"Track: %d/%d", songNumber+1, songCount];
	
	MPMediaItemArtwork *artwork = [item valueForProperty:MPMediaItemPropertyArtwork];
	CGSize aSize = currentArtwork.frame.size;
	[currentArtwork setImage:[artwork imageWithSize:aSize]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	songCount = 0;
	songNumber = 0;
	
	[self createCurrentTrackDisplay];
	[self createVolumeControl];
	[self createPrevNextButtons];
	[self createMediaPickerButton];
	
	player = [MPMusicPlayerController applicationMusicPlayer];
	
	[[NSNotificationCenter defaultCenter]
	 addObserver:self
	 selector:@selector(songChanged:)
	 name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
	 object:nil];
	
	player.beginGeneratingPlaybackNotifications;
}

- (void)dealloc {
	
	[[NSNotificationCenter defaultCenter]
	 removeObserver:self
	 name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
	 object:nil];
	
    [super dealloc];
}

@end
